package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.SysCodeitemBo;
import com.ruoyi.zhgd.domain.vo.SysCodeitemVo;
import com.ruoyi.zhgd.domain.SysCodeitem;
import com.ruoyi.zhgd.mapper.SysCodeitemMapper;
import com.ruoyi.zhgd.service.ISysCodeitemService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 参数配置Service业务层处理
 *
 * @author cj
 * @date 2023-09-21
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class SysCodeitemServiceImpl implements ISysCodeitemService {

    private final SysCodeitemMapper baseMapper;

    /**
     * 查询参数配置
     */
    @Override
    public SysCodeitemVo queryById(Long pkid){
        return baseMapper.selectVoById(pkid);
    }



    /**
     * 查询参数配置列表
     */
    @Override
    public List<SysCodeitemVo> queryList(SysCodeitemBo bo) {
        LambdaQueryWrapper<SysCodeitem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysCodeitem> buildQueryWrapper(SysCodeitemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysCodeitem> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCodeid()), SysCodeitem::getCodeid, bo.getCodeid());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), SysCodeitem::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getPptr()), SysCodeitem::getPptr, bo.getPptr());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), SysCodeitem::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription1()), SysCodeitem::getDescription1, bo.getDescription1());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription2()), SysCodeitem::getDescription2, bo.getDescription2());
        lqw.like(StringUtils.isNotBlank(bo.getShortname()), SysCodeitem::getShortname, bo.getShortname());
        lqw.eq(bo.getHasleaf() != null, SysCodeitem::getHasleaf, bo.getHasleaf());
        lqw.eq(StringUtils.isNotBlank(bo.getSpell()), SysCodeitem::getSpell, bo.getSpell());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysCodeitem::getStatus, bo.getStatus());
        lqw.eq(bo.getHashide() != null, SysCodeitem::getHashide, bo.getHashide());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderid()), SysCodeitem::getOrderid, bo.getOrderid());
        lqw.eq(bo.getSortid() != null, SysCodeitem::getSortid, bo.getSortid());
        lqw.eq(StringUtils.isNotBlank(bo.getTmpid()), SysCodeitem::getTmpid, bo.getTmpid());
        lqw.eq(StringUtils.isNotBlank(bo.getTmppid()), SysCodeitem::getTmppid, bo.getTmppid());
        lqw.eq(StringUtils.isNotBlank(bo.getZzbcode()), SysCodeitem::getZzbcode, bo.getZzbcode());
        lqw.eq(StringUtils.isNotBlank(bo.getPinyin()), SysCodeitem::getPinyin, bo.getPinyin());
        lqw.eq(StringUtils.isNotBlank(bo.getIcon()), SysCodeitem::getIcon, bo.getIcon());
        return lqw;
    }

    /**
     * 新增参数配置
     */
    @Override
    public Boolean insertByBo(SysCodeitemBo bo) {
        SysCodeitem add = BeanUtil.toBean(bo, SysCodeitem.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPkid(add.getPkid());
        }
        return flag;
    }

    /**
     * 修改参数配置
     */
    @Override
    public Boolean updateByBo(SysCodeitemBo bo) {
        SysCodeitem update = BeanUtil.toBean(bo, SysCodeitem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysCodeitem entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除参数配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<SysCodeitem> queryPageList(String codeid) {
        List<SysCodeitem> list =baseMapper.queryPageList(codeid);
        return list;
    }

    @Override
    public List<SysCodeitem> getCodeListAll(String codeid) {
        QueryWrapper<SysCodeitem> wrapper = new QueryWrapper<>();
        wrapper.eq("codeid", codeid).eq("hashide", 0).orderByAsc("orderid","code");
        return baseMapper.selectList(wrapper);
    }
}
